package com.wjh.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.MapPropertySource;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DynamicLoadPropertySource extends MapPropertySource {
    private static Map<String, Object> map = new ConcurrentHashMap<String, Object>(64);

    private static ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
    static {
        scheduled.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                //测试：定时修改value
                //真正使用可能需要传递一个参数或者定时读取配置文件
                map.put("test", "jms");
                System.out.println(map);
            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    public DynamicLoadPropertySource(String name, Map<String, Object> source) {
        super(name, map);
    }


    @Override
    public Object getProperty(String name) {
        return map.get(name);
    }
}

